/*
 * @Author: mengqingchen mengqingchen@huaru.cc
 * @Date: 2022-11-09 13:42:40
 * @LastEditors: mengqingchen mengqc1995@163.com
 * @LastEditTime: 2023-02-16 11:38:55
 * @FilePath: \sososn-web-new\src\utils\index.js
 * @Description:
 */

/**
 * 获取本地图
 * @param name // 文件名 如 doc.png
 * @returns {*|string}
 */
import { ElMessageBox } from 'element-plus'

import 'element-plus/theme-chalk/el-loading.css'
import 'element-plus/theme-chalk/el-message.css'
import 'element-plus/theme-chalk/el-notification.css'
import 'element-plus/theme-chalk/el-message-box.css'
import Cookies from 'js-cookie'
export function getAssetsImages(name) {
  return new URL(`/src/assets/images/${name}`, import.meta.url).href
}

/**
 * 获取字典label
 * @param arr,value
 * @returns {*|string}
 */
export function getLabel(arr, value) {
  let label = ''
  arr.forEach(element => {
    if (element.dictValue == value) {
      label = element.dictLabel
    }
  })
  return label
}
export function setToken(token) {
  return Cookies.set('Admin-Token', token)
}
export function setExpiresIn(time) {
  return Cookies.set('Admin-Expires-In', time)
}
export function GetUrlParam(paraName) {
  let url = document.location.toString()
  let arrObj = url.split('?')

  if (arrObj.length > 1) {
    let arrPara = arrObj[1].split('&')
    let arr

    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split('=')

      if (arr != null && arr[0] == paraName) {
        return arr[1]
      }
    }
    return ''
  }
  return ''
}
export function getToken() {
  return Cookies.get('Admin-Token') || false
}
export function removeToken() {
  return Cookies.remove('Admin-Token')
}
export function confirm(message, title, callback, btnConfirm = '确定') {
  ElMessageBox.confirm(message, title, {
    confirmButtonText: btnConfirm,
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      callback()
    })
    .catch(err => {
      console.log(err)
    })
}
/* 返回请求服务器路径 */
export function getPath() {
  return import.meta.env.VITE_API_BASEURL
    ? import.meta.env.VITE_API_BASEURL
    : window.location.origin + '/sn-api/'
}
